/**
 * 
 */
package com.example.radioclub.music.adapter;

import java.util.ArrayList;

import com.example.radioclub.R;
import com.example.radioclub.data.Constants;
import com.example.radioclub.types.SearchResult;
import com.example.radioclub.utilities.FunctionUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This adapter determine how search results will be showed in list, including
 * the layout and its behavior.
 * 
 * @author wizard
 * 
 */
public class SearchAdapter extends SimpleAdapter {
	private final String TAG = Constants.APP_TAG + "Search Adapter";
	private Context context;
	private ArrayList<SearchResult> array = null;

	public SearchAdapter(Context context, String searchResultJSON) {
		super(context, null, 0, new String[] {}, new int[] {});
		// TODO Auto-generated constructor stub
		this.context = context;
		this.array = FunctionUtils.getSearchResultArrayFromJSON(searchResultJSON);
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return this.array.get(position);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return array.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		SearchResult searchResult = this.array.get(position);

		View view = LayoutInflater.from(this.context).inflate(
				R.layout.music_layout, null);
		TextView songName = (TextView) view
				.findViewById(R.id.music_playlist_element_title), singerName = (TextView) view
				.findViewById(R.id.music_playlist_element_singer);

		songName.setText(searchResult.getSongName());
		singerName.setText(searchResult.getArtistName());

		ImageButton streamingButton = (ImageButton) view
				.findViewById(R.id.stream_this_song), addToPlaylistButton = (ImageButton) view
				.findViewById(R.id.add_this_to_playlist);

		streamingButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "Streaming!", Toast.LENGTH_LONG).show();
			}
		});

		addToPlaylistButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "Add to Playlist!", Toast.LENGTH_LONG)
						.show();
			}
		});

		return view;
	}

}
